package servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShoppingServlet
 */
@WebServlet("/ShoppingServlet")
public class ShoppingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShoppingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Integer> shopping;
		if(request.getSession().getAttribute("SHOPPING") != null){
			shopping = (Map<String, Integer>) request.getSession().getAttribute("SHOPPING");
		} else {
			shopping = new HashMap<String, Integer>();
		}
		
		String picUrl = request.getParameter("picUrl");
		if(shopping.get(picUrl) != null){
			int count = (int)shopping.get(picUrl);
			count++;
			shopping.put(picUrl,count);
		} else {
			shopping.put(picUrl,1);
		}
		request.getSession().setAttribute("SHOPPING", shopping);
		response.sendRedirect("shopping.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
